import { createRouter, createWebHistory } from 'vue-router'

import Login from '@/views/Login.vue'
import Main from '@/views/Main.vue'
import NotFound from '@/views/NotFound.vue'
import Forgot from '@/views/Forgot.vue'

// 动态导入子组件
const dynamicImports = {
    Forum: () => import('@/views/Forum.vue'),
    LunchDraw: () => import('@/views/LunchDraw.vue'),
    AIQA: () => import('@/views/AIQA.vue'),
    Friends: () => import('@/views/Friends.vue'),
    Profile: () => import('@/views/Profile.vue'),
    Announcements: () => import('@/views/Announcements.vue'),
    ForumPost: () => import('@/views/ForumPost.vue'),
}

const routes = [
    {
        path: '/',
        redirect: '/login' // 根路径重定向到登录页
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: {
            title: '校园登录',
            requiresAuth: false
        }
    },
    {
        path: '/forgot',
        name: 'Forgot',
        component: Forgot,
        meta: {
            title: '找回密码',
            requiresAuth: false
        }
    },
    {
        path: '/login-admin',
        name: 'LoginAdmin',
        component: () => import('@/views/LoginAdmin.vue'),
        meta: {
            title: '管理员登录',
            requiresAuth: false
        }
    },
    {
        path: '/main',
        name: 'Main',
        component: Main,
        meta: {
            title: '校园主页',
            requiresAuth: true
        },
        children: [
            {
                path: 'forum',
                name: 'Forum',
                component: dynamicImports.Forum,
                meta: {
                    title: '校园论坛',
                    requiresAuth: true
                }
            },
            {
                path: 'lunch-draw',
                name: 'LunchDraw',
                component: dynamicImports.LunchDraw,
                meta: {
                    title: '午饭抽签系统',
                    requiresAuth: true
                }
            },
            {
                path: 'ai-qa',
                name: 'AIQA',
                component: dynamicImports.AIQA,
                meta: {
                    title: '智能问答',
                    requiresAuth: true
                }
            },
            {
                path: 'friends',
                name: 'Friends',
                component: dynamicImports.Friends,
                meta: {
                    title: '好友动态',
                    requiresAuth: true
                }
            },
            {
                path: 'profile',
                name: 'Profile',
                component: () => import('@/views/Profile.vue'),
                meta: {
                    title: '个人中心',
                    requiresAuth: true
                }
            },
            {
                path: 'profile/edit',
                name: 'ProfileEdit',
                component: () => import('@/views/ProfileEdit.vue'),
                meta: {
                    title: '编辑资料',
                    requiresAuth: true
                }
            },
            {
                path: 'announcements',
                name: 'Announcements',
                component: dynamicImports.Announcements,
                meta: {
                    title: '校园公告',
                    requiresAuth: true
                }
            },
            {
                path: 'forum/post',
                name: 'ForumPost',
                component: dynamicImports.ForumPost,
                meta: { title: '发布动态', requiresAuth: true }
            },
            {
                path: 'friend-manage',
                name: 'FriendManage',
                component: () => import('@/views/FriendManage.vue'),
                meta: {
                    title: '好友管理',
                    requiresAuth: true
                }
            },
            {
                path: 'second-hand',
                name: 'SecondHand',
                component: () => import('@/views/index.vue'),
                meta: {
                    title: '二手平台',
                    requiresAuth: true
                }
            },
        ]
    },
    {
        path: '/me',
        name: 'Me',
        component: () => import('@/views/me.vue'),
        meta: { title: '个人中心', requiresAuth: true }
    },
    {
        path: '/message',
        name: 'Message',
        component: () => import('@/views/message.vue'),
        meta: { title: '消息', requiresAuth: true }
    },
    {
        path: '/order',
        name: 'Order',
        component: () => import('@/views/order.vue'),
        meta: { title: '订单详情', requiresAuth: true }
    },
    {
        path: '/release',
        name: 'Release',
        component: () => import('@/views/release.vue'),
        meta: { title: '发布二手物品', requiresAuth: true }
    },
    {
        path: '/search',
        name: 'Search',
        component: () => import('@/views/search.vue'),
        meta: { title: '搜索', requiresAuth: true }
    },
    {
        path: '/sign-in',
        name: 'SignIn',
        component: () => import('@/views/sign-in.vue'),
        meta: { title: '注册', requiresAuth: false }
    },
    {
        path: '/platform-admin',
        name: 'PlatformAdmin',
        component: () => import('@/views/platform-admin.vue'),
        meta: { title: '平台管理', requiresAuth: true }
    },
    {
        path: '/details',
        name: 'IdleDetails',
        component: () => import('@/views/idle-details.vue'),
        meta: { title: '物品详情', requiresAuth: true }
    },
    {
        path: '/index',
        name: 'Index',
        component: () => import('@/views/index.vue'),
        meta: { title: '首页', requiresAuth: true }
    },
    {
        path: '/user-list',
        name: 'UserList',
        component: () => import('@/views/common/userList.vue'),
        meta: { title: '用户管理', requiresAuth: true }
    },
    {
        path: '/:pathMatch(.*)*', // 捕获所有未匹配的路径
        name: 'NotFound',
        component: NotFound,
        meta: {
            title: '404 - 未找到页面',
            requiresAuth: false
        }
    }
]

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes,
    scrollBehavior(to, from, savedPosition) {
        return savedPosition || { top: 0 }
    }
})

// 路由守卫
router.beforeEach((to, from, next) => {
    if (window.__setAuthing) window.__setAuthing(true);
    const token = localStorage.getItem('token');
    if (to.path.startsWith('/main') && !token) {
        // 未登录访问主页面，强制跳转登录页
        next('/login');
    } else if ((to.path === '/login' || to.path === '/register') && token) {
        // 已登录访问登录/注册页，强制跳转主页面
        next('/main');
    } else {
        // 设置页面标题
        document.title = to.meta.title
            ? `${to.meta.title} | 校园平台`
            : '校园综合平台';
        next();
    }
})

router.afterEach(() => {
    if (window.__setAuthing) window.__setAuthing(false);
})

export default router